package com.yt.phone.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.phone.models.dto.HistoryDTO;
import com.yt.phone.models.dto.PhoneDTO;
import com.yt.phone.models.dto.VirtualphoneDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.phone.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.phone.mapper.HistoryMapper;
import com.yt.phone.models.service.IHistoryService;
import com.yt.phone.models.bo.HistorySaveBO;
import com.yt.phone.models.dto.HistoryDTO;

import java.util.List;

/**
 * 通话记录信息控制器
 *
 * @author lizicong
 * @Date 2025-11-05 23:39:45
 */
@DubboService
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, HistoryDTO> implements IHistoryService{

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private HistoryMapper historyMapper;

    @Override
    public boolean add(HistorySaveBO saveBO) {
        HistoryDTO dto = new HistoryDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }
    @Override
    public List<HistoryDTO> gethistoryList() {
        List<HistoryDTO> historyList = historyMapper.selectList(null);
        return historyList;
    }

    @Override
    public Page<HistoryDTO> getpagehistoryList(int currentPage) {
        // 1. 校验页码（最小为1）
        if (currentPage < 1) {
            currentPage = 1;
        }
        // 2. 定义每页条数
        int pageSize = 5;
        // 3. 计算分页起始位置
        int offset = (currentPage - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);

        EntityWrapper<HistoryDTO> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        // 4. 获取当前页数据（List类型）
        List<HistoryDTO> records = historyMapper.selectPage(rowBounds, wrapper);

        // 5. 查询总记录数
        int totalCount = historyMapper.selectCount(null);

        // 6. 手动封装 Page 对象（无需设置 pages，由 Page 自动计算）
        Page<HistoryDTO> page = new Page<>(currentPage, pageSize);
        page.setRecords(records); // 设置当前页数据
        page.setTotal(totalCount); // 设置总记录数（pages 会自动根据 total 和 size 计算）

        return page;
    }

    @Override
    public List<HistoryDTO> searchgethistoryList(String keyword) {
        Wrapper<HistoryDTO> wrapper = new EntityWrapper<>();
        wrapper.like("called_number", keyword)
                .or()
                .like("user_name", keyword);
        List<HistoryDTO> historyList = historyMapper.selectList(wrapper);
        return historyList;
    }
    @Override
    public List<HistoryDTO> getcallinghistoryByUserId(Long userId) {
        Wrapper<HistoryDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        List<HistoryDTO> historyList = historyMapper.selectList(wrapper);
        return historyList;
    }
    @Override
    public List<HistoryDTO> getcalledhistoryByUserId(List<String> userNumbers) {
        Wrapper<HistoryDTO> wrapper = new EntityWrapper<>();
        wrapper.in("called_number", userNumbers);
        List<HistoryDTO> historyList = historyMapper.selectList(wrapper);
        return historyList;
    }
}
